﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.IO;
using System.Xml;
using System.DirectoryServices.ActiveDirectory;
using System.Threading;
using System.Net;
using System.Text;
using System.Collections.ObjectModel;

namespace Main
{
    public class ControlInvoices
    {
        public class Model
        {
            public class Invoice
            {
                private int _customerid;
                private int _productid;
                private int _amount;
                private int _totalprice;
                private int _id;

                public Invoice()
                {

                }
                public int Id
                {
                    get { return _id; }
                    set { _id = value; }
                }
                public int Customerid
                {
                    get { return _customerid; }
                    set { _customerid = value; }
                }
                public int Productid
                {
                    get { return _productid; }
                    set { _productid = value; }
                }
                public int Amount
                {
                    get { return _amount; }
                    set { _amount = value; }
                }
                public int Totalprice
                {
                    get { return _totalprice; }
                    set { _totalprice = value; }
                }
            }
        }

        public class Control
        {
            ControlCommon common = new ControlCommon();

            /// <summary>
            /// saves invoice info on SQL Azure
            /// </summary>
            /// <param name="customerid"></param>
            /// <param name="productid"></param>
            /// <param name="amount"></param>
            /// <returns></returns>
            public String CreateInvoice(int customerid, int productid, int amount, string companyLogin)
            {
                try
                {
                    common.CreateLog("Starting saving of an Invoice on SQL Azure");
                    TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                    Invoice invoice = new Invoice();
                    invoice.customerno = customerid;
                    invoice.productno = productid;
                    invoice.amount = amount;
                    invoice.Company = companyLogin;

                    var query = from u in db.Products
                                where u.ID == productid && u.Company == companyLogin
                                select u.price;

                    invoice.totalprice = amount*query.SingleOrDefault();
                    
                    db.Invoices.InsertOnSubmit(invoice);
                    db.SubmitChanges();
                    db.Dispose();

                    common.CreateLog("Stopping Successful saving of an Invoice on SQL Azure");
                    return String.Empty;
                }
                catch (Exception e)
                {
                    common.CreateLog(e.ToString());
                    return e.ToString();
                }
            }

            /// <summary>
            ///  gets invoice info from SQL Azure if any
            /// </summary>
            /// <returns></returns>
            public List<Invoice> ViewInvoice(string companyLogin)
            {
                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                var query = from u in db.Customers
                            from o in u.Invoices
                            where u.ID == o.customerno && u.CompanyLogin == o.Company
                            select o;

                return query.ToList();
            }
        }
    }
}